﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.BUS;
using Restaurant.DTO;

namespace Restaurant
{
    public partial class frmMonAn : Form
    {
        private MonAnService maService = new MonAnService();

        public frmMonAn()
        {
            InitializeComponent();
        }

        public void LoadMonAn()
        {
            DataTable dbtab = maService.LoadMonAn();
            lv_ma.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[5].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                lv_ma.Items.Add(liv);
            }
        }

        private void frmMonAn_Load(object sender, EventArgs e)
        {
            LoadMonAn();
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            frmThemMonAn frm = new frmThemMonAn();
            frm.ShowDialog();
            LoadMonAn();
        }

        private void btn_thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_xoa_Click(object sender, EventArgs e)
        {
            try
            {
                int index = lv_ma.FocusedItem.Index;
                if (index >= 0)
                {
                    int ma = int.Parse(lv_ma.Items[index].Text);
                    if (maService.XoaMonAn(ma))
                    {
                        
                        LoadMonAn();
                        MessageBox.Show("Thành công");
                    }
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }

        private void btn_sua_Click(object sender, EventArgs e)
        {
            try
            {
                if (lv_ma.FocusedItem.Index >= 0)
                {
                    int index = lv_ma.FocusedItem.Index;
                    MonAn ma = maService.TimMonAn(int.Parse(lv_ma.Items[index].Text));
                    frmThemMonAn frm = new frmThemMonAn();
                    frm.btn_them.Text = "Sửa Món Ăn";
                    frm.flag = 1;
                    frm.ma = ma;
                    frm.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
    }
}
